﻿namespace ServerApp
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ServerApp.AdditionalClasses.External;

    public class ProducerService
    {
        public ProducerService()
        { 

        }

        /// <summary>
        /// Get all external producers
        /// </summary>
        /// <returns>list of External producers</returns>
        public static List<ExternalProducerView>GetProducers()
        {
            InternetShopBaseEntities efmodel = SingleEntity.GetInstance();

            //list of External producers
            List<ExternalProducerView> externalProducers = null;

            //if producers exist
            if (efmodel.Producers != null && efmodel.Producers.Count() > 0)
            {
                externalProducers = new List<ExternalProducerView>(); 

                foreach (Producer producer in efmodel.Producers)
                {
                    ExternalProducerView producerView = new ExternalProducerView()
                    {
                        Id = producer.ProducerID,
                        Name = producer.Name,
                        Description = producer.Description
                    };

                    //Add new External producer
                    externalProducers.Add(producerView);

                }//foreach producer

            }//efmodel.Producers != null

            return externalProducers;
        }

        /// <summary>
        /// Add new producer
        /// </summary>
        /// <param name="producer">New producer</param>
        /// <param name="producer">New producer</param>
        /// <returns>id of added producer. >1 - success, 0 - fail</returns>
        public static int AddProducer(ExternalProducerView externalProducer, List<long>productList)
        {            
            InternetShopBaseEntities efmodel = SingleEntity.GetInstance();

            //get the info from ExternalProducerView
            Producer producer = new Producer()
            {
                Name = externalProducer.Name,
                Description = externalProducer.Description
            };

            //Add new producer to db
            efmodel.AddToProducers(producer);
            efmodel.SaveChanges();

            //save producerId into variable
            int producerId = (int)producer.ProducerID;               

            //for each element of list of product ids
            for (int i = 0; i < productList.Count; i++)
            {
                //fill in the object ProductProducer with FK to Producer andFK to Product
                ProductProducer productProducer = new ProductProducer();
                productProducer.FK_ProducerId = producerId;
                productProducer.FK_ProductId = productList[i];

                //save to db
                efmodel.AddToProductProducers(productProducer);
            }
          
            efmodel.SaveChanges();
            efmodel.SaveChanges();

            //producer id changes after saving. 
            return producer.ProducerID;  
        }

        /// <summary>
        /// Update producer
        /// </summary>
        /// <param name="producer">Concrete producer</param>
        /// <returns>1 - updated successfuly, 0 - fail</returns>        
        public static int UpdateProducer(Producer producer)
        {
            InternetShopBaseEntities efmodel = SingleEntity.GetInstance();

            //get producer from db by id
            Producer updatedProducer = (from p in efmodel.Producers
                                        where p.ProducerID == producer.ProducerID
                                        select p).FirstOrDefault();

            //if such producerexists
            if (updatedProducer != null)
            {
                //do updating
                updatedProducer.Name = producer.Name;
                updatedProducer.Description = producer.Description;

                //save changes  
                efmodel.SaveChanges();

                //update succeeded
                return 1; 
            }

            //producer doesn't exist, update failed
            return 0;
        }

        /// <summary>
        /// Delete existing producer by id
        /// </summary>    
        public static int DeleteProducer(Producer producer)
        {
            if (producer == null)
            {
                return 0; //del fail. Nothing to delete
            }

            InternetShopBaseEntities efmodel = SingleEntity.GetInstance();

            //retrieve producer from db
            Producer producerDel = (from p in efmodel.Producers
                                    where p.ProducerID == producer.ProducerID
                                    select p).FirstOrDefault();

            //if producer exists
            if (producerDel != null)
            {
                //remove producer
                efmodel.DeleteObject(producerDel);
                efmodel.SaveChanges();

                return 1; //del success
            }

            return 0; //del fail
        }        
    }//class
}
